﻿using System.Collections.Generic;

namespace Sidvall.Logging
{
    public class LogCategoryCollection : System.Collections.ObjectModel.KeyedCollection<string, LogCategory>
    {
        #region Public Members

        #region Add

        public void Add(string category, IEnumerable<EventType> eventTypes)
        {
            var item = new LogCategory()
            {
                Name = category,
            };
            if (eventTypes != null)
            {
                foreach (var eventType in eventTypes)
                    item.EnabledEventTypes.Add(eventType);
            }
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<string> categories, IEnumerable<EventType> eventTypes)
        {
            if (categories == null)
                return;
            foreach (var category in categories)
                Add(category, eventTypes);
        }

        #endregion
        #region Contains

        public bool Contains(IEnumerable<string> categories, EventType severity)
        {
            if (categories == null)
                return false;
            foreach (var category in categories)
            {
                if (Contains(category))
                {
                    var item = this[category];
                    if (item.EnabledEventTypes.Contains(severity))
                        return true;
                }
            }
            return false;
        }

        #endregion
        #region GetKeyForItem

        protected override string GetKeyForItem(LogCategory item)
        {
            if (item == null)
                throw new System.ArgumentNullException(nameof(item));
            return item.Name;
        }

        #endregion

        #endregion
        #region Constructors

        public LogCategoryCollection() : base(System.StringComparer.OrdinalIgnoreCase)
        {

        }

        #endregion
    }
}
